package com.example.recuitactivity.candidate_fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.recuitactivity.myMainActivity.CandidateMainActivity;
import com.example.recuitactivity.CollectionActivity;
import com.example.recuitactivity.helper.DbOpenHelper;
import com.example.recuitactivity.R;
import com.example.recuitactivity.bean.Resume;

import java.util.ArrayList;
import java.util.List;

public class CandidateResumeFragment extends Fragment {
    private  String username;
    private long candidates_id;
    private Resume resume;

    private View messageLayout;
    private Button btn_save,btn_update;
    private EditText et_name,et_school,et_age,et_tele,et_project_exp,et_reward_exp;
    private TextView tv_name,tv_sex,tv_school,tv_age,tv_tele,tv_edubg,tv_project_exp,tv_reward_exp;
    private Spinner spinner_sex,spinner_edubg;
    private TextView tv_number_delivered,tv_number_interviews,tv_number_collection;
    private CandidateMainActivity activity=null;



    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch(view.getId()){
                case R.id.btn_save_resume:
                    saveInfo();
                    changeToUnwriteable();
                    break;
                case R.id.btn_update_resume:
                    changeToWriteable();
                    break;
                case R.id.tv_number_delibvered:
                    if(activity==null)
                        activity=(CandidateMainActivity) getActivity();
                    activity.changeFragment(1);
                    break;
                case R.id.tv_number_interviews:
                    if(activity==null)
                        activity=(CandidateMainActivity) getActivity();
                    activity.changeFragment(2);
                    break;
                case R.id.tv_number_collection:
                    Intent intent=new Intent(getContext(), CollectionActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putLong("candidates_id",candidates_id);
                    bundle.putString("username",username);
                    intent.putExtras(bundle);
                    getContext().startActivity(intent);
                    break;
            }
        }
    };




    public void initUI() {
        tv_number_delivered=(TextView)messageLayout.findViewById(R.id.tv_number_delibvered);
        tv_number_interviews=(TextView)messageLayout.findViewById(R.id.tv_number_interviews);
        tv_number_collection=(TextView)messageLayout.findViewById(R.id.tv_number_collection);
        btn_save=(Button) messageLayout.findViewById(R.id.btn_save_resume);
        btn_update=(Button) messageLayout.findViewById(R.id.btn_update_resume);
        et_name=(EditText) messageLayout.findViewById(R.id.et_resume_name);
        et_school=(EditText) messageLayout.findViewById(R.id.et_resume_school);
        et_age=(EditText) messageLayout.findViewById(R.id.et_resume_age);
        et_tele=(EditText) messageLayout.findViewById(R.id.et_resume_tele);
        et_project_exp=(EditText) messageLayout.findViewById(R.id.et_resume_project_exp);
        et_reward_exp=(EditText) messageLayout.findViewById(R.id.et_resume_reward_exp);
        tv_name=(TextView) messageLayout.findViewById(R.id.tv_resume_name);
        tv_sex=(TextView) messageLayout.findViewById(R.id.tv_resume_sex);
        tv_school=(TextView) messageLayout.findViewById(R.id.tv_resume_school);
        tv_age=(TextView) messageLayout.findViewById(R.id.tv_resume_age);
        tv_tele=(TextView) messageLayout.findViewById(R.id.tv_resume_tele);
        tv_edubg=(TextView) messageLayout.findViewById(R.id.tv_resume_edubg);
        tv_project_exp=(TextView) messageLayout.findViewById(R.id.tv_resume_project_exp);
        tv_reward_exp=(TextView) messageLayout.findViewById(R.id.tv_resume_reward_exp);
        List<String> list_sex=new ArrayList<String>();
        list_sex.add("请选择");
        list_sex.add("男");
        list_sex.add("女");
        ArrayAdapter<String> adapter_sex = new ArrayAdapter<String>(
                getActivity(), android.R.layout.simple_list_item_1, list_sex );
        spinner_sex=(Spinner) messageLayout.findViewById(R.id.s_resume_sex);
        spinner_sex.setAdapter(adapter_sex);

        List<String> list_edubg=new ArrayList<String>();
        list_edubg.add("请选择");
        list_edubg.add("高中");
        list_edubg.add("大专");
        list_edubg.add("本科");
        list_edubg.add("硕士");
        list_edubg.add("博士");
        ArrayAdapter<String> adapter_edubg = new ArrayAdapter<String>(
                getActivity(), android.R.layout.simple_list_item_1, list_edubg );
        spinner_edubg=(Spinner) messageLayout.findViewById(R.id.s_resume_edubg);
        spinner_edubg.setAdapter(adapter_edubg);

        btn_save.setOnClickListener(listener);
        btn_update.setOnClickListener(listener);
        tv_number_delivered.setOnClickListener(listener);
        tv_number_interviews.setOnClickListener(listener);
        tv_number_collection.setOnClickListener(listener);

    }

    public void initDates(){
        DbOpenHelper dbOpenHelper=new DbOpenHelper(getContext());
        candidates_id=dbOpenHelper.getCandidateIdByUsername(username);
        resume= dbOpenHelper.getResumeInfoById(candidates_id);

        int deliveredNum=dbOpenHelper.getDeliveredNumByCid(candidates_id);
        int interviewNum=dbOpenHelper.getInterviewsNumByCid(candidates_id);
        int collectionNum=dbOpenHelper.getCollectsNumByCid(candidates_id);
        tv_number_delivered.setText(String.valueOf(deliveredNum));
        tv_number_interviews.setText(String.valueOf(interviewNum));
        tv_number_collection.setText(String.valueOf(collectionNum));

        tv_name.setText(resume.getName());
        tv_sex.setText(resume.getSex());
        tv_school.setText(resume.getSchool());
        tv_age.setText(resume.getAge()==0? "":String.valueOf(resume.getAge()));
        tv_tele.setText(resume.getTelephone());
        tv_edubg.setText(resume.getEdu_background());
        tv_project_exp.setText(resume.getProject_exp());
        tv_reward_exp.setText(resume.getReward_exp());
        changeToUnwriteable();
    }

    public void saveInfo(){
        DbOpenHelper dbOpenHelper=new DbOpenHelper(getContext());
        String name=et_name.getText().toString();
        String sex=spinner_sex.getSelectedItem().toString();
        long sex_id=spinner_sex.getSelectedItemId();
        String school=et_school.getText().toString();
        String age=et_age.getText().toString();
        String tele=et_tele.getText().toString();
        String edubg=spinner_edubg.getSelectedItem().toString();
        long edubg_id=spinner_edubg.getSelectedItemId();
        String project_exp=et_project_exp.getText().toString();
        String reward_exp=et_reward_exp.getText().toString();
        resume.setName(name);
        resume.setSex(sex_id==0?"":sex);
        resume.setSchool(school);
        resume.setAge(age.length()!=0?Integer.valueOf(age):0);
        resume.setTelephone(tele);
        resume.setEdu_background(edubg_id==0?"":edubg);
        resume.setProject_exp(project_exp);
        resume.setReward_exp(reward_exp);

        //更新界面数据
        tv_name.setText(resume.getName());
        tv_sex.setText(resume.getSex());
        tv_school.setText(resume.getSchool());
        tv_age.setText(resume.getAge()==0? "":String.valueOf(resume.getAge()));
        tv_tele.setText(resume.getTelephone());
        tv_edubg.setText(resume.getEdu_background());
        tv_project_exp.setText(resume.getProject_exp());
        tv_reward_exp.setText(resume.getReward_exp());
        changeToUnwriteable();

        //向数据库中更新数据
        dbOpenHelper.updateResume(resume);
    }

    //将简历由不可修改转为可修改
    public void changeToWriteable(){
        String[] sex_options={"男","女"};
        String[] edubg_options={"高中","大专","本科","硕士","博士"};

        btn_save.setVisibility(View.VISIBLE);
        btn_update.setVisibility(View.INVISIBLE);

        et_name.setVisibility(View.VISIBLE);
        et_name.setText(tv_name.getText().toString());
        tv_name.setVisibility(View.INVISIBLE);

        spinner_sex.setVisibility(View.VISIBLE);
        for(int i=0;i<2;i++){
            if(sex_options[i].equals(tv_sex.getText().toString()))
                spinner_sex.setSelection(i+1);
        }
        tv_sex.setVisibility(View.INVISIBLE);

        et_school.setVisibility(View.VISIBLE);
        et_school.setText(tv_school.getText().toString());
        tv_school.setVisibility(View.INVISIBLE);

        et_age.setVisibility(View.VISIBLE);
        et_age.setText(tv_age.getText().toString());
        tv_age.setVisibility(View.INVISIBLE);

        et_tele.setVisibility(View.VISIBLE);
        et_tele.setText(tv_tele.getText().toString());
        tv_tele.setVisibility(View.INVISIBLE);

        spinner_edubg.setVisibility(View.VISIBLE);
        for(int i=0;i<5;i++){
            if(edubg_options[i].equals(tv_edubg.getText().toString()))
                spinner_edubg.setSelection(i+1);
        }
        tv_edubg.setVisibility(View.INVISIBLE);

        et_project_exp.setVisibility(View.VISIBLE);
        et_project_exp.setText(tv_project_exp.getText().toString());
        tv_project_exp.setVisibility(View.INVISIBLE);

        et_reward_exp.setVisibility(View.VISIBLE);
        et_reward_exp.setText(tv_reward_exp.getText().toString());
        tv_reward_exp.setVisibility(View.INVISIBLE);
    }

    //将简历由可修改转为不可修改
    public void changeToUnwriteable(){
        btn_save.setVisibility(View.INVISIBLE);
        btn_update.setVisibility(View.VISIBLE);

        tv_name.setVisibility(View.VISIBLE);
        et_name.setVisibility(View.INVISIBLE);
        tv_sex.setVisibility(View.VISIBLE);
        spinner_sex.setVisibility(View.INVISIBLE);
        tv_school.setVisibility(View.VISIBLE);
        et_school.setVisibility(View.INVISIBLE);
        tv_age.setVisibility(View.VISIBLE);
        et_age.setVisibility(View.INVISIBLE);
        tv_tele.setVisibility(View.VISIBLE);
        et_tele.setVisibility(View.INVISIBLE);
        tv_edubg.setVisibility(View.VISIBLE);
        spinner_edubg.setVisibility(View.INVISIBLE);
        tv_project_exp.setVisibility(View.VISIBLE);
        et_project_exp.setVisibility(View.INVISIBLE);
        tv_reward_exp.setVisibility(View.VISIBLE);
        et_reward_exp.setVisibility(View.INVISIBLE);
    }

    public CandidateResumeFragment(String username){
        this.username=username;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        messageLayout = inflater.inflate(R.layout.fragment_candidate_resume, container, false);
        initUI();
        initDates();
        return messageLayout;
    }

    @Override
    public void onResume() {
        super.onResume();
        initDates();
    }
}